package com.iawg.attrition.game;

import java.util.ArrayList;
import java.util.List;

import com.iawg.attrition.engine.BasicEngine;
import com.iawg.attrition.engine.messaging.BasicMessageHandler;
import com.iawg.attrition.engine.renderer.ArdorSWTRenderer;
import com.iawg.attrition.engine.renderer.BasicRenderer;
import com.iawg.attrition.game.map.Map;
import com.iawg.attrition.game.units.Actor;

public class GameEngine extends BasicEngine {
	//public static GameEngine ENGINE = new GameEngine(new ConsoleRenderer(), new MessageHandler());
	public static GameEngine ENGINE = new GameEngine(new ArdorSWTRenderer(), new BasicMessageHandler());	

	private Map map;
	private List<Actor> actorList = new ArrayList<Actor>();
	
	public GameEngine(BasicRenderer attritionRenderer, BasicMessageHandler messageHandler) {
		super(attritionRenderer, messageHandler);
	}

	@Override
	protected void load(Object resource) {
		//If the resource is an instance of map, then set the map as the given map.
		if(resource instanceof Map)
			map = (Map) resource;
	  //If the resource is not a map and map is null, then error because there is no place to load other resources.
		else if(map == null)
			throw new IllegalStateException("No map has been initialized.");
	  // If the resource is an actor and the map is loaded, then load the resource to the Actor Array List.
		if(resource instanceof Actor)
			actorList.add((Actor) resource);
		
	}

	@Override
	protected void release(Object resource){
	  // If the resource is the map, clear everything.
		if(resource instanceof Map){
			map = null;
			actorList = new ArrayList<Actor>();
	  // If the resource is not a map and the map is null, then error because there should be no loaded resources.
		}else if(map == null)
			throw new IllegalStateException("No map has been initialized.");
		/*
		 *  If the resource is an actor, the attempt to remove it from the actor array list.
		 *  If the object is not in the list, then error because there was no resource to release.
		 */
		if(!actorList.remove(resource))
			throw new NullPointerException("Requested reasource does not exist.");
	}
}
